#include "app_data.h"
#include <stdio.h>
#include <ctype.h>

#include "delay.h"
#include "app.h"
#include "at24c16.h"
#include "at24c16_p.h"


SettingsTypeDef    settings;

///****************************************************************************
char toUpper(char c)
{
	char r_c = c;
	if ( c >= 'a' && c <= 'z' )
	{
		r_c -= 0x20;
	}
	return r_c;
}

u8 getHexFromChr(char c)
{
	u8 hex = 0;
	c = toUpper(c);
    if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F'))
    {
    	hex = (c >= 'A') ? c - 'A' + 10 : c - '0';
    }
    return hex;
}


void Data_Reset(void)
{
	//u8 i = 0;
	settings.version[0] = MAJOR_VER;
	settings.version[1] = MINOR_VER;
	settings.ver = getHexFromChr(settings.version[0]);
	settings.ver <<= 4;
	settings.ver |= getHexFromChr(settings.version[1]);
	settings.machine_id = 01;
	settings.wakeup_time = 60;

    DWR(settings);
}

//u8 buf[3]={0};
void Data_Init(void)
{
	AT24C_Init((I2C_SW_Handle*)&_at24c16_hanlde);
//		AT24C_Write(0,"12",2);
//		AT24C_Read(0,buf,2);

	DRD(settings);

	if ( settings.version[0] != MAJOR_VER ||
         settings.version[1] != MINOR_VER)
	{
		Data_Reset();
		_delay_ms(50);
	}
}



